//
//  ModifyDefaultParametersViewController.m
//  ForAddSDK
//
//  Created by liboxiang on 2018/1/18.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "ModifyDefaultParametersViewController.h"
#import "DataHandler.h"

@interface ModifyDefaultParametersViewController ()<NSTableViewDataSource,NSTableViewDelegate>

@property (weak) IBOutlet NSTableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;

@end

@implementation ModifyDefaultParametersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    _dataArray = [DataHandler getDefaultValuesArray];
    _tableView.rowHeight = 25;
}
- (void)deleteRow:(NSButton *)button 
{
    [_dataArray removeObjectAtIndex:button.tag];
    [_tableView beginUpdates];
    [_tableView removeRowsAtIndexes:[NSIndexSet indexSetWithIndex:button.tag] withAnimation:NSTableViewAnimationSlideDown];
    [_tableView endUpdates];
}
- (IBAction)addRow:(id)sender {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    NSArray *columnIDArray = COLUMNID_ARRAY;
    for (NSString *key in columnIDArray) {
        [dic setObject:@"" forKey:key];
    }
    [_dataArray addObject:dic];
    [_tableView beginUpdates];
    [_tableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:_dataArray.count -1] withAnimation:NSTableViewAnimationSlideDown];
    [_tableView endUpdates];
}

- (IBAction)saveValue:(id)sender {
    [_dataArray removeAllObjects];
    NSInteger count = _tableView.numberOfRows;
    NSArray *columnsArray = _tableView.tableColumns;
    for (int i = 0; i < count; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
        for (NSTableColumn *column in columnsArray) {
            if (![column.identifier isEqualToString:COLUMNID_CANCEL]) {
                NSInteger index = [columnsArray indexOfObject:column];
                NSTextField *textField = (NSTextField *)[_tableView viewAtColumn:index row:i makeIfNecessary:NO];
                NSString *value = @"";
                if (textField) {
                    value = DEL_SPACE(textField.stringValue);
                }
                [dic setObject:value forKey:column.identifier];
            }
        }
        [_dataArray addObject:dic];
    }
    if (_dataArray.count == _tableView.numberOfRows) {
        [DataHandler saveDefaultValuesArray:_dataArray];
        [[NSNotificationCenter defaultCenter] postNotificationName:DefaultDataChangeNotification object:@""];
    }
}

#pragma mark NSTableViewDataSource

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return _dataArray.count;
}

#pragma mark NSTableViewDelegate

- (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
    if ([tableColumn.identifier isEqualToString:COLUMNID_CANCEL]) {
        NSButton *button = [NSButton buttonWithTitle:@"删除" target:self action:@selector(deleteRow:)];
        button.tag = row;
        return button;
    }
    else {
        NSTextField *textField = [NSTextField new];
        NSDictionary *dic = _dataArray[row];
        if ([dic.allKeys containsObject:tableColumn.identifier]) {
            textField.stringValue = [_dataArray[row] objectForKey:tableColumn.identifier];
        }
        return textField;
    }
}

@end
